package com.gem.service.impl;

import com.gem.domain.CCrParticipant;
import com.gem.domain.CompanyContact;
import com.gem.mapper.CCrParticipantMapper;
import com.gem.service.CCrParticipantService;
import com.gem.service.CompanyContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description: 参与人中间表接口实现
 * create 2019/10/30 15:03
 * Modified By:
 */
@Service
public class CCrParticipantServiceImpl implements CCrParticipantService {

    //自动注入CCrParticipantMapper
    private CCrParticipantMapper cCrParticipantMapper;

    @Autowired
    public void setcCrParticipantMapper(CCrParticipantMapper cCrParticipantMapper) {
        this.cCrParticipantMapper = cCrParticipantMapper;
    }

    //自动注入
    CompanyContactService companyContactService;

    @Autowired
    public void setCompanyContactService(CompanyContactService companyContactService) {
        this.companyContactService = companyContactService;
    }

    //查询所有
    @Override
    public List<CCrParticipant> selectAllCPService() {
        return cCrParticipantMapper.selectAllCP();
    }

    //按编号查询
    @Override
    public CCrParticipant selectCPByIdService(Integer cpId) {
        return cCrParticipantMapper.selectCPById(cpId);
    }

    //按企业-合作成果编号查询
    @Override
    public List<CCrParticipant> selectCPByccIdService(Integer ccId) {
        return cCrParticipantMapper.selectCPByccId(ccId);
    }

    //按企业-合作成果编号查所有参与人
    @Override
    public List<CompanyContact> selectConByccIdService(Integer ccId) {
        return cCrParticipantMapper.selectConByccId(ccId);
    }

    //添加
    @Override
    @Transactional
    public boolean addCPService(CompanyContact companyContact, String ccId) {
        try {
            CompanyContact companyContactSearch = companyContactService.selectConByName(companyContact.getCoContactName());
            Integer tempConId = 0;
            if (companyContactSearch == null) {
                if (companyContact.getCoContactName() != null) {
                    if (companyContactService.save(companyContact) > 0) {
                        CompanyContact companyContactNew = companyContactService.selectConByName(companyContact.getCoContactName());
                        tempConId = companyContactNew.getCoContactId();
                    } else {
                        return false;
                    }
                }
            } else {
                tempConId = companyContactSearch.getCoContactId();
            }
            List<CCrParticipant> cCrParticipantList = selectCPByccIdService(Integer.parseInt(ccId));
            boolean cpFlag = true;
            for (CCrParticipant cCrParticipant : cCrParticipantList) {
                if (tempConId.equals(cCrParticipant.getCcpConId())) {
                    cpFlag = false;
                    break;
                }
            }
            if (cpFlag) {
                CCrParticipant cCrParticipant = new CCrParticipant();
                assert companyContactSearch != null;
                cCrParticipant.setCcpConId(tempConId);
                cCrParticipant.setCcpCcId(Integer.parseInt(ccId));
                if (cCrParticipantMapper.addCP(cCrParticipant)) {
                    return true;
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    //单个删除
    @Override
    public boolean deleteCPService(Integer cpId) {
        return cCrParticipantMapper.deleteCP(cpId);
    }

    //批量删除
    @Override
    @Transactional
    public boolean deleteCPsService(String[] cpIdArr) {
        try {
            boolean flag = true;
            if (cpIdArr.length != 0) {
                for (String cpId : cpIdArr) {
                    if (!cpId.trim().equals("")) {
                        if (!deleteCPService(Integer.parseInt(cpId))) {
                            flag = false;
                            break;
                        }
                    }
                }
                if (!flag) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
}
